﻿#region Using Statements
using System;
using GameBase.Cameras;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Implementation of a head-up display.
    /// </summary>
    public partial class HeadUpDisplay : ScreenComponent2D, IDisposable
    {
        #region Fields

        GraphicsDevice GraphicsDevice;
        ICamera camera;
        SpriteSheet spriteSheet;
        SpriteFont smallFont;

        #endregion

        #region Properties

        /// <summary>
        /// Indicates whether the head-up display is visible.
        /// </summary>
        public bool Visible
        {
            private get;
            set;
        }

        #endregion

        #region Screen Transitions

        bool toVisible = false;
        bool toHidden = false;
        float trTime = 0.0f;

        /// <summary>
        /// Makes the ScreenComponent2D transition to visible state.
        /// </summary>
        public override void Show()
        {
            toVisible = true;
        }

        /// <summary>
        /// Makes the ScreenComponent2D transition to hidden state.
        /// </summary>
        public override void Hide()
        {
            toHidden = true;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent2D is associated to.</param>
        public HeadUpDisplay(Screen screen)
            : base(screen)
        {
            spriteSheet = Screen.Content.Load<SpriteSheet>(ContentSettings.GetAsset("HUD_SPRITES"));
            smallFont = Game.Content.Load<SpriteFont>(ContentSettings.GetAsset("FONT_SMALL"));

            camera = Game.Services.GetService(typeof(ICamera)) as ICamera;
            if (camera == null)
                throw new InvalidOperationException("Service of type ICamera not found");

            GraphicsDevice = Screen.GraphicsDevice;

            HeadUpDisplay_Bars();
            HeadUpDisplay_CrossHair();
            HeadUpDisplay_Score();
            HeadUpDisplay_Ship();
            HeadUpDisplay_Effects();
        }

        #endregion

        #region Dispose

        // dispose flag
        bool isDisposed = false;

        /// <summary>
        /// Immediately releases all resources used by this object.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
        /// </summary>
        ~HeadUpDisplay()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by this object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (fuelBar != null) { fuelBar.Dispose(); fuelBar = null; }
                    if (healthBar != null) { healthBar.Dispose(); healthBar = null; }
                    if (boostBar != null) { boostBar.Dispose(); boostBar = null; }
                    if (bombModeBar != null) { bombModeBar.Dispose(); bombModeBar = null; }
                }

                isDisposed = true;
            }
        }

        #endregion
    }
}
